<template>
	<div>
		<CommonSearch v-model="tableConfig.searchParams" :config="searchConfig"></CommonSearch>
		<CommonTable :config="tableConfig" ref="commonTable"></CommonTable>
	</div>
</template>

<script lang="javascript">
	import { reactive, toRefs, onMounted, getCurrentInstance } from 'vue';
	import { CREATE, UPDATE, DELETE, QUERY, IMPORT } from '@/utils/Const';

	import {
		getPostListApi,
		createPostApi,
		updatePostApi,
		deletePostApi
	} from '@/api/system.api';
	import { noNumberToNull } from '@/utils/validate';
	export default {
		name : 'Jobs',
		setup () {
			let Instance = getCurrentInstance().proxy;
			let commonTable = null;
			let data = reactive({
				searchConfig : {
					handlerSearch () {
						return commonTable.fetchData({ isResetPageNumber : true });
					},
					form : [
						{
							type : 'text',
							label : '岗位编码:',
							placeholder : '请输入岗位编码',
							key : 'postCode'
						},
						{
							type : 'text',
							label : '岗位名称:',
							placeholder : '请输入岗位名称',
							key : 'postName'
						},
						{
							type : 'select',
							label : '状态:',
							placeholder : '请选择状态',
							key : 'status',
							options : [
								{
									value : '',
									label : '全部'
								},
								{
									value : '1',
									label : '正常'
								},
								{
									value : '0',
									label : '停用'
								}
							]
						}
					]
				},
				tableConfig : {
					searchParams : {},
					reqApiMethod : getPostListApi,
					handlerOperate : (payload, row) => {
						data.formConfig.type = payload.butType;
						let formConfig = {
							data : Array.isArray(row) ? row[0] : '',
							...data.formConfig
						};

						let names = [];
						let ids = [];
						switch (payload.butType) {
						case CREATE:
							Instance.$hyDialogForm(data.formConfig);
							break;
						case DELETE:
							row.forEach((item) => {
								names.push(item.postName);
								ids.push(item.postId);
							});
							Instance.$hyConfirm({
								subTitle : `是否确定删除【${names.join()}】`,
								callback : async (done) => {
									await deletePostApi({ ids : ids.join() });
									done();
									commonTable.fetchData();
								}
							});
							break;
						case UPDATE:
							Instance.$hyDialogForm(formConfig);
							break;
						case QUERY:
							formConfig.disabled = true;
							Instance.$hyDialogForm(formConfig);
							break;
						}
					},
					mergeTable : () => {},
					columns : [
						{
							type : 'index'
						},
						{
							type : 'selection'
						},
						{
							title : '岗位编号',
							field : 'postId'
						},
						{
							title : '岗位编码',
							field : 'postCode'
						},
						{
							title : '岗位名称',
							field : 'postName'
						},
						{
							title : '岗位排序',
							field : 'postSort'
						},
						{
							title : '状态',
							field : 'status',
							formatter : (value) => {
								if (Number(value) === 0) {
									return '停用';
								}
								return '正常';
							}
						},
						{
							title : '操作时间',
							field : 'createTime'
						}
					],
					globalOperate : [
						{
							butType : CREATE,
							name : '新增',
							color : 'primary',
							role : [],
							icon : 'el-icon-plus'
						},
						{
							butType : DELETE,
							name : '删除',
							enable : '2',
							color : 'danger',
							role : [],
							icon : 'el-icon-delete'
						},
						{
							butType : UPDATE,
							name : '修改',
							enable : '1',
							color : 'success',
							role : [],
							icon : 'el-icon-edit'
						},
						{
							butType : QUERY,
							name : '查看',
							enable : '1',
							color : 'warning',
							role : [],
							icon : 'el-icon-edit'
						},
						{
							butType : IMPORT,
							name : '导入',
							color : 'el-icon-upload2',
							role : [],
							icon : 'el-icon-upload2'
						}
					],
					operate : [
						{
							butType : UPDATE,
							type : 'text',
							name : '修改',
							role : [],
							icon : 'el-icon-edit'
						},
						{
							butType : DELETE,
							type : 'text',
							name : '删除',
							role : [],
							icon : 'el-icon-delete'
						}
					]
				},
				formConfig : {
					type : '',
					async handlerSubmit (params) {
						switch (data.formConfig.type) {
						case CREATE:
							await createPostApi(params);
							break;
						case UPDATE:
							await updatePostApi(params);
							break;
						}
						return commonTable.fetchData();
					},
					form : [
						{
							type : 'text',
							label : '岗位Id',
							key : 'postId',
							hide : true
						},
						{
							type : 'text',
							label : '岗位名称:',
							placeholder : '请输入岗位名称',
							maxlength : 10,
							key : 'postName',
							rules : [
								{ required : true, message : '请输入岗位名称', trigger : 'blur' }
							]
						},
						{
							type : 'text',
							label : '岗位编码:',
							maxlength : 8,
							placeholder : '请输入岗位编码',
							key : 'postCode',
							rules : [
								{ required : true, message : '请输入岗位编码', trigger : 'blur' }
							]
						},
						{
							type : 'text',
							label : '岗位顺序:',
							placeholder : '请输入岗位顺序',
							key : 'postSort',
							maxlength : 2,
							handlerLimiting : (val) => {
								return noNumberToNull(val);
							},
							rules : [
								{ required : true, message : '请输入岗位顺序', trigger : 'blur' }
							]
						},
						{
							type : 'radio',
							label : '岗位状态:',
							key : 'status',
							defaultValue : '1',
							options : [
								{
									value : '1',
									label : '正常'
								},
								{
									value : '0',
									label : '停用'
								}
							]
						},
						{
							type : 'textarea',
							label : '备注:',
							placeholder : '请输入内容',
							key : 'remark',
							maxlength : 100
						}
					]
				}
			});

			onMounted(() => {
				commonTable = Instance.$refs['commonTable'];
			});
			return {
				...toRefs(data)
			};
		}
	};
</script>

<style lang="scss" scoped></style>
